package airthmetic.exercise.linkedList;

import java.util.HashSet;

public class _142_环形链表II {
    public static ListNode detectCycle(ListNode head) {
        if(head == null || head.next == null){
            return null;
        }
        HashSet<ListNode> cache = new HashSet<ListNode>();
        ListNode current = head;
        while(current != null){
            if(cache.contains(current)){
                return current;
            }else{
                cache.add(current);
            }
            current = current.next;
        }
        return null;
    }


    public static void main(String[] args) {


        ListNode listNode4 = new ListNode(4, null);
        ListNode listNode3 = new ListNode(3, listNode4);
        ListNode listNode2 = new ListNode(2, listNode3);
        ListNode listNode1 = new ListNode(1, listNode2);

    }
}
